<%
    Users user = Trader.getUser(request.getParameter("uid"));
	wstrader.task.traffic.Physical ftask = user.traffic.physical;
	wstrader.task.traffic.physical.Contract task = user.traffic.physical.contract;
	if (!user.Authenticated()) {
		response.sendRedirect("logon.jsp");
		return;
	}
	((Tasks)ftask.getOwner()).clearFocus();
	ftask.setFocus(true);	
	Trader.window_width = "90%";	
	ResultSet rs;
%>

<%@include file="../header.jsp"%>

<%
	float sal_ton =0;
	float pur_ton =0;
	float sal_delvd =0;
	float sal_pend =0;
	float pur_delvd =0;
	float pur_pend =0;
%>

<link href="style.css" rel="stylesheet" type="text/css">

<script language="javascript">
function doAddNew()
{
	var form = document.formMain;
	form.new_rec.value = "1";
	form.action = "traffic.physical.contract.card.jsp";
	form.submit();
}



function doFilter()
{
	var form = document.formMain;
	form.action = "traffic.physical.jsp";
	form.submit();
}

function clickCardView()
{
	var form = document.formMain;
	form.action = "traffic.physical.contract.card.jsp";
	form.submit();	
}

function cardViewClick(row)
{
	var form = document.formMain;
	form.contracts.value = row.id;
	dblClick(row);
}
</script>
<style type="text/css">
<!--
.style3 {font-family: Arial}
.style6 {font-size: 12px}
.style12 {font-family: Arial; font-size: 12px; }
-->
</style>
<form action="" method="post" name="formMain" id="formMain">
  <table width="100%"  border="0" cellspacing="0" cellpadding="0">
    <tr>
      <th colspan="2" align="left" scope="col"><img src="images/contracts.jpg" width="250" height="30"></th>
      <th width="5%" scope="col">&nbsp;</th>
      <th width="5%" scope="col">&nbsp;</th>
      <th colspan="4" scope="col"><input type="hidden" name="uid"        id="uid"       value="<%=user.getuid()%>">
        <input type="hidden" name="task_id"    id = "task_id"   value="0">
        <input name="type" type="hidden" id="type">
        <input name="contracts" type="hidden" id="contracts" value="0">
        <input name="new_rec" type="hidden" id="new_rec" value="0"></th>
    </tr>
    <tr align="left">
      <td colspan="8">
	    <table>
    <tr>
      <td width="150" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Contract Type </span></strong></td>
      <td width="150" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Origin</span></strong></td>
      <td align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Quality</span></strong></td>
      <td align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Grade</span></strong></td>
      <td align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Buyer/Seller</span></strong></td>
      <td align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Last Date </span></strong></td>
    </tr>
    <tr>
      <td height="32">
	  <select name="cnt_type" size="9" id="cnt_type" onChange="doFilter()" style="font-family:Arial; font-size:11px; width:150px;border:0">
        <option value="0" selected>All</option>
        <option value="P">Purchase</option>
        <option value="S">Sales</option>
		<%task.getResponse().setSelected("cnt_type");%>
      </select>
	  </td>
      <td>
	  <select name="origin" size="9" id="origin" onChange="doFilter()" style="font-family:Arial; font-size:11px; width:150px;border:0">
		  <option value="0" selected>All</option>
	  	<%
			task.getResponse().setOption("origin_master","name");
			task.getResponse().setSelected("origin");
		%>	  
      </select>
	  </td>
      <td><select name="quality" size="9" id="quality" onChange="doFilter()" style="font-family:Arial; font-size:11px; width:160px;border:0">
        <option value="0" selected>All</option>
        <%
			task.getResponse().setOption("quality_master","name");
			task.getResponse().setSelected("quality");
		%>
      </select></td>
      <td>
	  <select name="grade" size="9" id="grade" onChange="doFilter()" style="font-family:Arial; font-size:11px; width:160px;border:0">
	  	<option value="0" selected>All</option>
	  	<%
			task.getResponse().setOption("grade_master","name");
			task.getResponse().setSelected("grade");
		%>      
	  </select>
	  </td>
      <td>
	  <select name="buyer_seller" size="9" id="buyer_seller" onChange="doFilter()" style="font-family:Arial; font-size:11px; width:150px;border:0">
		  <option value="0" selected>All</option>
	  	<%
			task.getResponse().setOption(task.getBuyerSellerList());
			task.getResponse().setSelected("buyer_seller");
		%>	  
      </select>
	  </td>
      <td>
	  <select name="ls_date" size="9" id="ls_date" onChange="doFilter()" style="font-family:Arial; font-size:11px; width:150px;border:0">
		<%@include file="include/ContractLsDateList.jsp"%>
      </select>
	  </td>
    </tr>
	  
	  </table>	  </td>
    </tr>
    <tr>
      <td colspan="8">	  <table width="100%"  border="1" cellspacing="1" cellpadding="1">
        <tr bordercolor="#EEEEEE">
          <td width="2%" rowspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">&nbsp;</span></strong><strong><span class="style29 style3 style6">Id</span></strong></td>
          <td width="8%" rowspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Ref.No</span></strong></td>
          <td width="8%" rowspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Brk.No</span></strong></td>
          <td width="9%" rowspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">&nbsp;</span></strong><strong><span class="style29 style3 style6">Origin</span></strong></td>
          <td width="15%" rowspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">&nbsp;</span></strong><strong><span class="style29 style3 style6">Grade</span></strong></td>
          <td width="12%" rowspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">&nbsp;</span></strong><strong><span class="style29 style3 style6">Supplier / Buyer </span></strong></td>
          <td colspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">&nbsp;</span></strong><strong><span class="style29 style3 style6">Shipping Period &nbsp;</span></strong></td>
          <td colspan="3" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">&nbsp;</span></strong><strong><span class="style29 style3 style6">Tons &nbsp;</span></strong></td>
          <td colspan="2" align="center" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Allocation</span></strong><strong><span class="style29 style3 style6"></span></strong></td>
        </tr>
        <tr bordercolor="#FFFBF0">
          <td width="8%" align="center" bordercolor="#EEEEEE" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">First Dt. </span></strong></td>
          <td width="8%" align="center" bordercolor="#EEEEEE" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Last Dt. </span></strong></td>
          <td width="6%" align="center" bordercolor="#EEEEEE" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Total</span></strong></td>
          <td width="6%" align="center" bordercolor="#EEEEEE" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Deliverd</span></strong></td>
          <td width="5%" align="center" bordercolor="#EEEEEE" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Balance</span></strong></td>
          <td width="7%" align="center" bordercolor="#EEEEEE" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Alc</span></strong></td>
          <td width="6%" align="center" bordercolor="#EEEEEE" bgcolor="#EEEEEE"><strong><span class="style29 style3 style6">Unalc</span></strong></td>
        </tr>
        <tr>
          <td colspan="13">
            <div id=scroll1 style="BORDER:0px ; OVERFLOW: auto; WIDTH: 100%; HEIGHT: 260px;">
<table width="100%" height = "22"  border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFBF0">
<%
int dLoop=0;
ResultSet Rs = task.fetchContractList();
int R_id = 0;
String Colour="";
float pending = 0;
float unfixed = 0;
float alcton =0;
float balalcton =0;
while (Rs.next())
{
		R_id = Rs.getInt("id");
		pending = Rs.getFloat("Tons")-Rs.getFloat("receive_tons");
		unfixed = Rs.getFloat("unfixed_tons");
		if ((pending > 0) && (unfixed > 0)) Colour = "#FF0000";
		if ((pending == 0) && (unfixed > 0)) Colour = "#FFFF66";
		if ((pending > 0) && (unfixed == 0)) Colour = "#33CCFF";
		if ((pending == 0) && (unfixed == 0)) Colour = "#000011";
		
		if (Rs.getString("type").compareTo("S")==0)
		{
			alcton=task.getSum("allocated_tons","contract_sid","contract_allocation",Rs.getInt("id"));
			balalcton = Rs.getFloat("tons")-alcton;
		}
		else
		{
			alcton=task.getSum("allocated_tons","contract_pid","contract_allocation",Rs.getInt("id"));
			balalcton = Rs.getFloat("tons")-alcton;		
		}
%>
        <tr id=<%=R_id%>  onDblClick="cardViewClick(this);" onClick="highlightOn(this);">
          <td width="2%" height="20" class="style11" align="center" class="style11" ><%=Rs.getInt("id")%></td>
          <td width="8%" align="center" class="style11" style="color:<%=Colour%>"><%=Rs.getString("ref_number")%></td>
          <td width="8%" class="style11" style="color:<%=Colour%>" align="center"><%=Rs.getString("contract_no")%></td>		  
          <td width="9%" class="style11 "align="left" ><%=task.getFieldValueForId("origin_master","name",Rs.getInt("origin_id"))%></td>
          <td width="15%" class="style11" align="left" ><%=task.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"))%></td>
          <td width="12%" class="style11" align="left" ><%=task.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id"))%></td>
          <td width="8%" class="style11" align="center" ><%=Utils.dateToStr(Rs.getDate("first_date"))%></td>
          <td width="8%" class="style11" align="center" ><%=Utils.dateToStr(Rs.getDate("last_date"))%></td>
          <td width="6%" class="style11" align="right" ><%=Utils.numberToStr(Rs.getFloat("tons"),3)%></td>
		  <%
		  if (Rs.getString("type").compareTo("P")==0) 
		  {
		  	pur_ton=pur_ton + Rs.getFloat("tons");
			pur_delvd=pur_delvd + Rs.getFloat("receive_tons");
			pur_pend=pur_pend + (Rs.getFloat("Tons")-Rs.getFloat("receive_tons"));
		  }
		  if (Rs.getString("type").compareTo("S")==0) 
		  {
		  	sal_ton=sal_ton + Rs.getFloat("tons");
			sal_delvd=sal_delvd + Rs.getFloat("receive_tons");
			sal_pend=sal_pend + (Rs.getFloat("Tons")-Rs.getFloat("receive_tons"));
		  }
		  
		  %>
          <td width="6%" class="style11" align="right" ><%=Utils.numberToStr(Rs.getFloat("receive_tons"),3)%></td>		  
          <td width="5%" class="style11" align="right" ><%=Utils.numberToStr(Rs.getFloat("Tons")-Rs.getFloat("receive_tons"),3)%></td>
          <td width="7%" class="style11" align="right" ><%=Utils.numberToStr(alcton,3)%></td>		  
          <td width="6%" class="style11" align="right"><%=Utils.numberToStr(balalcton,3)%></td>
        </tr>
<%
		dLoop=dLoop+1;
}
%>
</table>
          </div></td>
        </tr>
      </table>
    </tr>
    <tr>
      <td width="12%">&nbsp;</td>
      <td width="8%">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td width="4%" align="right">&nbsp;&nbsp;</td>
      <td width="5%" align="right">&nbsp;</td>
      <td width="14%" align="right">&nbsp;</td>
      <td width="47%" align="right"><table width="100%"  border="0.5" cellspacing="0" cellpadding="0">
        <tr>
          <td><strong><span class="style12">Total Purchase </span></strong></td>
          <td align="right" class="style12"><strong><%=Utils.numberToStr(pur_ton,3)%></strong></td>
          <td align="right" class="style12"><strong><%=Utils.numberToStr(pur_delvd,3)%></strong></td>
          <td align="right" class="style12"><strong><%=Utils.numberToStr(pur_pend,3)%></strong></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="28%"><strong><span class="style12">Total Sales </span></strong></td>
          <td width="15%" align="right" class="style12"><strong><%=Utils.numberToStr(sal_ton,3)%></strong></td>
          <td width="14%" align="right" class="style12"><strong><%=Utils.numberToStr(sal_delvd,3)%></strong></td>
          <td width="13%" align="right" class="style12"><strong><%=Utils.numberToStr(sal_pend,3)%></strong></td>
          <td width="30%">&nbsp;</td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td align="right" valign="middle">&nbsp;&nbsp;</td>
      <td align="right" valign="middle">&nbsp;</td>
      <td align="right" valign="middle">&nbsp;</td>
      <td align="right" valign="middle"><img src="images/cardview.jpg" width="55" height="18" onClick="clickCardView();">&nbsp;</td>
    </tr>
  </table>
</form>
<%@include file="../footer.jsp"%>

